package com.zlm.IO.ByteStream.ZipInputStream;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;

/**
 * @author 13026
 */
public class ZipInputStream_demo {
    /*
     * ZipInputStream
     * 解压缩流
     * */
    public static void main(String[] args) throws IOException {
        // 1.创建一个File表示要解压的压缩包
        File src = new File("D:\\myProject\\javaTest\\oneModule\\src\\com\\zlm\\IO\\ByteStream\\ZipStream\\FilesPackage\\FilesPackage.zip");
        // 2.创建一个File表示解压的目的地
        File dest = new File("D:\\myProject\\javaTest\\oneModule\\src\\com\\zlm\\IO\\ByteStream\\ZipStream\\FilesPackage");
        unzip(src, dest);

    }

    public static void unzip(File src, File dest) throws IOException {
        // 解压的本质：把压缩包里面每个文件/文件夹读取出来，按层拷贝到目的地
        // 创建一个解压缩流来读取压缩包中的数据
        ZipInputStream zis = new ZipInputStream(new FileInputStream(src));
        // 先获取到压缩包里每一个ZipEntry对象，代表文件/文件夹
        ZipEntry entry;
        while ((entry = zis.getNextEntry()) != null) {
            System.out.println(entry);
            if(entry.isDirectory()){
                File file = new File(dest, entry.toString());
                file.mkdirs();
            }else{
                FileOutputStream fos = new FileOutputStream(new File(dest, entry.toString()));
                int b;
                while ((b = zis.read()) != -1){
                    fos.write(b);
                }
                fos.close();
            }
        }
        // 记得关流
        zis.close();
    }
}
